'Copyright 2006 James Downey

Imports FetchXML
Imports FetchXMLBuilder.MetadataServiceSdk

Public Class frmRelationship

    Public Relationship As RelationshipMetadata
    Private Entity As EntityMetadata
    Public RelationshipType As RelationshipType

    Public Sub New(ByVal Entity As EntityMetadata)
        InitializeComponent()
        Me.Entity = Entity
    End Sub

    Private Sub frmRelationship_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim rel As RelationshipMetadata
        For Each rel In Entity.ReferencesTo
            Me.lstRelationship.Items.Add(rel.Name)
        Next
        For Each rel In Entity.ReferencesFrom
            Me.lstRelationship.Items.Add(rel.Name)
        Next
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If Me.lstRelationship.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a relationship.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim rel As RelationshipMetadata
        Dim name As String = Me.lstRelationship.SelectedItem.ToString
        For Each rel In Entity.ReferencesTo
            If rel.Name = name Then
                Me.Relationship = rel
                Me.RelationshipType = FetchXMLBuilder.RelationshipType.ToRelationship
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
                Exit Sub
            End If
        Next
        For Each rel In Entity.ReferencesFrom
            If rel.Name = name Then
                Me.Relationship = rel
                Me.RelationshipType = FetchXMLBuilder.RelationshipType.FromRelationship
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
                Exit Sub
            End If
        Next
    End Sub

End Class

Public Enum RelationshipType
    ToRelationship
    FromRelationship
End Enum